//
//  GjjContentView.swift
//  BMort_SwiftUI
//
//  Created by anscen on 2022/9/1.
//

import SwiftUI

struct BGjjContentView: View {
    @State var money :String = ""
    @State var month :String = ""
    @State var rate  :String = ""
    
    @State var index :Int    = 0
    private var listData :[String] = ["等额本息","等额本金"]
    var body: some View {
        VStack {
            Spacer(minLength: 5)
            HStack {
                Picker(selection:$index) {
                    ForEach(0..<listData.count) { index in
                        Text(self.listData[index]).tag(index)
                    }
                } label: {
                    
                }.pickerStyle(SegmentedPickerStyle()).frame(width: 150, height: 36, alignment: .center)
            }
            Spacer(minLength: 5)
            List {
                BTextFieldView(title: moya.money, placeholder: "输入贷款金额", text: $money)
                BTextFieldView(title: moya.month, placeholder: "输入贷款月数", text: $month)
                BRateView(state: .gjjMort, title: moya.rate, rate: $rate)
                BComputerView(content: {
                    BMoreContentView(model: self.getMortGage())
                })
            }.padding(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 0)).listStyle(.insetGrouped)
        }.onAppear {
            self.loadData()
        }
        
    }
    func getMortGage() ->BMortgage{
        debugPrint("BMoreContentView")
        var item = BMortgage()
        item.identy = "\(BAppMortState.gjjMort.rawValue)"
        item.gjjmoney = self.money
        item.month = self.month
        item.gjjrate  = self.rate
        item.repay = self.index == 0 ? .rate : .capital
        if self.month.count > 0 && self.money.count > 0 && self.rate.count > 0{
            BMortData.updateData(model: item) { success in

            }
        }
        return item
    }
    func loadData(){
        if self.money.count == 0{
            BMortData.getData(identy: "\(BAppMortState.gjjMort.rawValue)") { model in
                if let item = model{
                    self.money = item.gjjmoney ?? ""
                    self.month = item.month ?? ""
                    self.rate = item.gjjrate ?? ""
                    self.index = item.repay == .rate ? 0 : 1
                }
            }
        }
    }
}
struct BTextFieldView :View {
    @State   var title       :String = ""
    @State   var placeholder :String = ""
    @Binding var text        :String
    var body: some View{
        HStack {
            Text(title).font(Font(UIFont.systemFont(ofSize: 16))).foregroundColor(Color.appx000000())
            Spacer(minLength: 10)
            TextField(placeholder, text: $text).frame(width: 120, height: 36, alignment: .trailing)
        }.padding(EdgeInsets(top: 4, leading: 0, bottom: 4, trailing: 0))
    }
}
struct BRateView :View{
    public   var state       :BAppMortState
    public   var title       :String = ""
    @Binding var rate        :String
    var body: some View{
        HStack {
            NavigationLink {
                BRateContentView(state: state, rate: $rate )
            } label: {
                Text(title).font(Font(UIFont.systemFont(ofSize: 16))).foregroundColor(Color.appx000000())
                Text(rate).frame(width: 100, height: 36, alignment: .trailing)
            }
        }.padding(EdgeInsets.init(top: 4, leading: 0, bottom: 4, trailing: 0))
    }
}
struct BComputerView<Content :View> :View{
    let content: Content
    @State var push :Bool = false
    init(@ViewBuilder content: () -> Content) {
        self.content = content()
    }
    var body: some View{
        ZStack.init {
            HStack {
                Text("计算").font(.system(size: 16)).foregroundColor(.white).background(Color.appColor())
            }.padding(EdgeInsets(top: 12, leading:100, bottom: 12, trailing: 100)).background(Color.appColor()).cornerRadius(5)
            NavigationLink(destination: content) {
                
            }.opacity(0)
        }.padding(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 0))
    }
}
